﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using Configuration.Templating.Context;

namespace Aricie.LuceneSearch.Tests.Components.Config.Templating
{
    public class DNNTemplatingContextTest
    {
        [Fact]
        public void ICanCreateDNNTemplatingContexts()
        {
            var DNNTC = new DNNTemplatingContext("TypeTemplate");
            var DNNTCC = new DNNTemplatingContext("TypeTemplate") { CurrentCulture = "CurrentCulture"};
            var DNNTCCF = new DNNTemplatingContext("TypeTemplate"){ CurrentCulture = "CurrentCulture"};
            var DNNTCCFK = new DNNTemplatingContext("TypeTemplate") { CurrentCulture = "CurrentCulture", Content = "Content" };
        }

        public class TheSeparator
        {
            [Fact]
            public void IsTakenIntoAccountToDetermineTheKey()
            {
                var DNNTCC = new DNNTemplatingContext("TypeTemplate") { CurrentCulture = "CurrentCulture" };
                DNNTCC.Separator = "|";
                Assert.Equal("TypeTemplate|CurrentCulture", DNNTCC.Key);
            }
        }

        [Fact]
        public void ParentChainWorksFine()
        {
            /* everything is filled */
            TestExpectations(new DNNTemplatingContext("TypeTemplateContentAndCulture") { CurrentCulture = "CurrentCulture", Content = "Contenu" }, 
                new List<string>(new string[] {
                    "TypeTemplateContentAndCulture.Contenu.CurrentCulture",
                    "TypeTemplateContentAndCulture.Contenu",
                    "TypeTemplateContentAndCulture.CurrentCulture",
                    "TypeTemplateContentAndCulture"
                }));
            
            /* pas de culture */
            TestExpectations(new DNNTemplatingContext("TypeTemplateContent") { Content = "Contenu"},
                new List<string>(new string[] {
                    "TypeTemplateContent.Contenu",
                    "TypeTemplateContent"
                }));

            TestExpectations(new DNNTemplatingContext("TypeTemplateAlone") { },
                new List<string>(new string[] {
                    "TypeTemplateAlone"
                }));

            /* culture */

            TestExpectations(new DNNTemplatingContext("TypeTemplateCulture") { CurrentCulture = "CurrentCulture"},
                new List<string>(new string[] {
                    "TypeTemplateCulture.CurrentCulture", 
                    "TypeTemplateCulture"
                }));


        }

        private void TestExpectations(DNNTemplatingContext DTC, IEnumerable<string> results )
        {
            ITemplatingContext CurrentNode = DTC;
            foreach (string result in results)
            {
                Assert.Equal(result, CurrentNode.Key);
                CurrentNode = CurrentNode.GetParentContext();
            }
            Assert.Null(CurrentNode);
        }
        
    }
}
